package com.alasbusy.alasbusyapp.activity.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import ch.ielse.view.imagewatcher.ImageWatcher;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.twoactivity.ReleaseActivity;
import com.alasbusy.alasbusyapp.base.BaseFragment;
import com.alasbusy.highperformancefriendscirclelibrary.adapters.FriendCircleAdapter;
import com.alasbusy.highperformancefriendscirclelibrary.beans.FriendCircleBean;
import com.alasbusy.highperformancefriendscirclelibrary.interfaces.OnPraiseOrCommentClickListener;
import com.alasbusy.highperformancefriendscirclelibrary.others.DataCenter;
import com.alasbusy.highperformancefriendscirclelibrary.others.FriendsCircleAdapterDivideLine;
import com.alasbusy.highperformancefriendscirclelibrary.others.GlideSimpleTarget;
import com.alasbusy.highperformancefriendscirclelibrary.utils.Utils;
import com.alasbusy.highperformancefriendscirclelibrary.widgets.EmojiPanelView;
import com.bumptech.glide.Glide;
import com.zhy.android.percent.support.PercentLinearLayout;
import io.reactivex.Single;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import java.util.List;

/**
 * Created by Administrator on 2018/5/30.
 */

public class SocialFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,
        OnPraiseOrCommentClickListener, ImageWatcher.OnPictureLongPressListener, ImageWatcher.Loader {

    private FloatingActionButton social_action_btn;
    private RecyclerView social_recycler;

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private Disposable mDisposable;
    private FriendCircleAdapter mFriendCircleAdapter;
    private ImageWatcher mImageWatcher;
    private static EmojiPanelView mEmojiPanelView;

    @Override
    public int getLayout() {
        return R.layout.social_fragment;
        //return R.layout.activity_main_library;
    }

    @Override
    protected void initView(View view) {
        PercentLinearLayout myself_fragment_title = view.findViewById(R.id.social_fragment_title);
        setTop(myself_fragment_title);
        getVisibility(false, getActivity());
        getTitle("社交");

        social_action_btn = view.findViewById(R.id.social_action_btn);


        mEmojiPanelView = view.findViewById(R.id.emoji_panel_view);
        mEmojiPanelView.initEmojiPanel(DataCenter.emojiDataSources);
        mSwipeRefreshLayout = view.findViewById(R.id.swpie_refresh_layout);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        //        findViewById(R.id.img_back).setOnClickListener(v ->
        //                startActivity(new Intent(MainActivity.this, EmojiPanelActivity.class)));

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Glide.with(getContext()).resumeRequests();
                } else {
                    Glide.with(getActivity()).pauseRequests();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mImageWatcher = view.findViewById(R.id.image_watcher);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new FriendsCircleAdapterDivideLine());
        mFriendCircleAdapter = new FriendCircleAdapter(getContext(), recyclerView, mImageWatcher);
        recyclerView.setAdapter(mFriendCircleAdapter);
        mImageWatcher.setTranslucentStatus(Utils.calcStatusBarHeight(getActivity()));
        mImageWatcher.setErrorImageRes(R.mipmap.error_picture);
        mImageWatcher.setOnPictureLongPressListener(this);
        mImageWatcher.setLoader(this);
        Utils.showSwipeRefreshLayout(mSwipeRefreshLayout, this::asyncMakeData);
    }

    private void asyncMakeData() {
        mDisposable = Single.create((SingleOnSubscribe<List<FriendCircleBean>>) emitter ->
                emitter.onSuccess(DataCenter.makeFriendCircleBeans(getActivity())))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((friendCircleBeans, throwable) -> {
                    Utils.hideSwipeRefreshLayout(mSwipeRefreshLayout);
                    if (friendCircleBeans != null && throwable == null) {
                        mFriendCircleAdapter.setFriendCircleBeans(friendCircleBeans);
                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        social_action_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), ReleaseActivity.class));
            }
        });

        mEmojiPanelView.setSendInCommentClick(new EmojiPanelView.sendInComment() {
            @Override
            public void setOnSendClick(String content) {
                Toast.makeText(getContext(),content,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void setOnPrompt() {
                Toast.makeText(getContext(),"当前未输入任何内容.",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public void onRefresh() {
        asyncMakeData();
    }

    //赞
    @Override
    public void onPraiseClick(int position) {
        Toast.makeText(getActivity(), "You Click Praise!", Toast.LENGTH_SHORT).show();
    }


    //评论
    @Override
    public void onCommentClick(int position) {
        //        Toast.makeText(this, "you click comment", Toast.LENGTH_SHORT).show();
        mEmojiPanelView.showEmojiPanel();
    }

    @Override
    public void onPictureLongPress(ImageView v, String url, int pos) {

    }


    @Override
    public void load(Context context, String url, ImageWatcher.LoadCallback lc) {
        Glide.with(context).asBitmap().load(url).into(new GlideSimpleTarget(lc));
    }

    public static EmojiPanelView getEmojiPanelView() {
        return mEmojiPanelView;
    }

    @Override
    public void onPause() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        Utils.hideSwipeRefreshLayout(mSwipeRefreshLayout);
        super.onPause();
    }

    @Override
    public void onStop() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        Utils.hideSwipeRefreshLayout(mSwipeRefreshLayout);
        super.onStop();

    }
}
